<?php

    function createJsonData($arr){
        $arrData = array("code"=>0,"msg"=>"");
        $arrData["count"] = count($arr);
        $arrData["data"]  = $arr;
        return $arrData;
    }

    function success_return($msg) {
        $arrData = array("code"=>0,"msg"=>"" , "count"=>0 , "data"=>array("status"=>"ok" , "message"=>$msg));
        
        return json_encode($arrData);
    }


    function error_return($msg) {
        $arrData = array("code"=>0,"msg"=>"" , "count"=>0 , "data"=>array("status"=>"ok" , "message"=>$msg));
        
        return json_encode($arrData);
    }


    // // 获取文件后缀
    function get_file_type($filename){
        $type = substr($filename, strrpos($filename, ".")+1);
        return $type;
    }

    function shangchuan () {
        $file = $_FILES["uploadFile"];
        $name = $file["name"];
        $tmp_path = $file["tmp_name"];
        $time = time() * 4;
        $str = strval($time);
        $fileTyep = get_file_type($name);
        $fullName = $str.".".$fileTyep;
        $dirPath = "uploadPath/".$fullName;
        if(file_exists($dirPath)){
            return "";

        } else {
            move_uploaded_file($tmp_path, $dirPath);
            return $dirPath;
        }

    }


    // 通过code值获取openid
    function get_openid_by_code () {
        $code = $_POST["code"];
        $get_code_url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx5fe83a28a6b1d08c&secret=63a4f34c316868e9e84033610ab7d7a0&js_code=$_POST[code]&grant_type=authorization_code";
        
        $data = getFn($get_code_url);

        $data = json_decode($data);
        
        if (!$data->session_key) {
            echo error_return("获取openid失败");
        } else {
            $openid = $data->openid;
            $session_key = $data->session_key;
            $time = time();
            $status_code = $itme *3;

            $time = (string)$time;
            $status_code = (string)$status_code;
            mysql_query("insert into wx_session_key 
            (session_key , openid , status_code , update_time) 
            values ('$session_key' , '$openid' , '$status_code' , '$time')");
            echo json_encode(createJsonData(array("openid"=>$openid , "status_code"=>$status_code)));
        }

    }

    // 通过 status_code 获取 openid
    function get_openid_by_status_code() {
        $status_code = $_POST["status_code"];
        $code = $_POST["code"];

        $result = mysql_query("select * from wx_session_key where status_code='$status_code'");

        $results = array();
        $sqlTime = "";
        $openid = "";
        while ($row = mysql_fetch_assoc($result)) {
            $results[] = $row;
            $sqlTime = $row["update_time"];
            $openid = $row["openid"];
        }
        if (count($results) <= 0) {
            // 没有找到 status_code 值
            echo error_return("status_code出错");
            return;
        }

        $current_time = time();
        $sqlTime = (int)$sqlTime;

        if ($current_time < $sqlTime + 7000) {
            // 没过期
            echo json_encode(createJsonData(array("openid"=>$openid , "status_code"=>$status_code)));
        } else {
            
            $get_code_url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx5fe83a28a6b1d08c&secret=63a4f34c316868e9e84033610ab7d7a0&js_code=$_POST[code]&grant_type=authorization_code";
            
            $data = getFn($get_code_url);
    
            $data = json_decode($data);

            $openid = $data->openid;
            $session_key = $data->session_key;
            $time = time();

            $time = (string)$time;
            mysql_query("update wx_session_key set 
            session_key='$session_key' , openid='$openid' , status_code , update_time='$time' 
            where status_code='$status_code'");
            echo json_encode(createJsonData(array("openid"=>$openid , "status_code"=>$status_code)));
        }
    }



    function getFn($url) {
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);//输入自己的微信公众号APPID和secret
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //不验证证书
      
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //显示获得的数据
        return $data;
    }


  
?>